package com.example.cfm880.layouttransitions;

import android.animation.LayoutTransition;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

/**
 * This example shows how to use LayoutTransition to animate simple changes in a layout
 * container.
 */

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button addButton = (Button) findViewById(R.id.addButton);
        Button removeBUtton = (Button) findViewById(R.id.removeButton);
        final LinearLayout container = (LinearLayout) findViewById(R.id.container);

        final Context context = this;

        // Start with two views
        for (int i = 0; i < 2; ++i){
            container.addView(new ColorView(this));
        }
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Adding a view will cause a LayoutTransition animation
                container.addView(new ColorView(context), 1);
            }
        });


        removeBUtton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Removing a view will cause a LayoutTransition animation
                container.removeViewAt(Math.min(1, container.getChildCount() - 1));
            }
        });

        // Note that this assumes a LayoutTransition is set on the container, which is the
        // case here because the container has the attribute "animateLayoutChanges" set to true
        // in the layout file. You can also call setLayoutTransition() in
        // code to set a LayoutTransition on any container
        LayoutTransition transition = container.getLayoutTransition();

        // New capability as of Jellybean; monitor the container for *all* layout changes
        // (not just add/remove/visibility changes) and animation these changes as well.
        transition.enableTransitionType(LayoutTransition.CHANGING);

    }

    private static class ColorView extends View {

        private boolean mExpanded = false;
        private LayoutParams mCompressedParams = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 50);

        private LayoutParams mExpandedParams = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 200);

        /**
         * Custom view painted with a random background color and two different sizes which are
         * toggled between due to user interaction
         */
        public ColorView(Context context) {
            super(context);
            int red = (int) (Math.random() * 128 + 127);
            int green = (int) (Math.random() * 128 + 127);
            int blue = (int) (Math.random() * 128 + 127);
            int color = 0xff << 24 | (red << 16) | (green << 8) | blue;
            setBackgroundColor(color);
            setLayoutParams(mCompressedParams);
            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Size changes will cause a LayoutTransition animation if the CHANGING
                    //transition is enabled
                    setLayoutParams(mExpanded ? mCompressedParams:mExpandedParams);
                    mExpanded = !mExpanded;
                    requestLayout();
                }
            });
        }
    }

}
